#include "rows.h"

#include <QVariant>

#include "generic_bitwise.h"


namespace BitWin
{


//---- Anonymous namespace for internal usage staff

namespace
{

// Get a middle part of the string.
// Part bounds are given by the range; if the range
// exceeds the string bounds, the result is padded by
// spaces.
QString paddedMiddle(const QString& s, const BitRange& range)
{
    return s
        .mid(range.begin(), range.length())
        .leftJustified(range.length(), ' ')
    ;
}

} // Anonymous namespace end


//---- FieldRow class

FieldRow::FieldRow(
    const QString& title_, const BitRange& range_, const int word_no_
):
    Row(title_, word_no_),
    _range(range_)
{}

QVariant FieldRow::data(
    role_t role, const BitRange& range_, const PGenericBitwise& data_
) const
{
    if (BitwiseDataRole == role)
        return (QVariant(paddedMiddle(data_->bitwise(_range), range_)));
    else
        return QVariant();
}


//---- WordRow class

WordRow::WordRow(const QString& title_, const int word_no_):
    Row(title_, word_no_)
{}

QVariant WordRow::data(
    role_t role, const BitRange& range_, const PGenericBitwise& data_
) const
{
    switch(role) // HACK
    {
        case role_t::BitwiseDataRole:
            return QVariant(paddedMiddle(data_->bitwise(), range_));
        case role_t::HexDataRole:
            return QVariant(data_->hex());
        case role_t::DecDataRole:
            return QVariant(data_->dec());
        default:
            return QVariant();
    }
}


} // BitWin namespace end
